<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category Information.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/5/23  17:11
 * +--------------------------------------------------------+
 */

namespace App\Services\QueryList\Components;

use App\Jobs\CollectArticleJob;
use App\Models\CollectArticle;
use App\Repositories\CollectArticleRepository;
use App\Services\DingService;
use App\Services\QueryList\QueryList;
use App\Util\DingDing;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class Information extends QueryList
{
    protected $collectArticleRepository;

    public function __construct()
    {
        $this->collectArticleRepository = new CollectArticleRepository(new CollectArticle());
    }

    /**
     * @return bool
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/6/17  19:25
     */
    public function collect36kr()
    {
        $time     = Carbon::now()->toDateString();
        $site_url = 'https://36kr.com';
        $url      = $site_url . '/hot-list/renqi/' . $time . '/1';

        $reg = [
            //采集文章标题
            'title' => ['.title-wrapper', 'text'],
            'href'  => ['.title-wrapper>a', 'href'],
        ];

        $rang = '.article-list .article-wrapper';
        $data = $this->collect($url, $reg, $rang);

        $this->collect_redis_set($data, $site_url, 1);
        return true;
    }

    public function collectHuxiu()
    {
        $site_url = 'https://www.huxiu.com';
        $url      = [
            $site_url . '/channel/106.html',
            $site_url . '/channel/103.html',
            $site_url . '/channel/105.html',
            $site_url . '/channel/115.html',
            $site_url . '/article/',
        ];

        foreach ($url as $v) {
            $reg  = [
                //采集文章标题
                'title' => ['h5', 'text'],
                'href'  => ['a', 'href'],
            ];
            $rang = '.channel-items .article-item';
            $data = $this->collect($v, $reg, $rang);
            $this->collect_redis_set($data, $site_url, 2);
        }

        return true;
    }

    public function collectTmtpost()
    {
        $site_url = 'https://www.tmtpost.com';
        $url      = $site_url;

        $reg  = [
            //采集文章标题
            'title' => ['a', 'text'],
            'href'  => ['a', 'href'],
        ];
        $rang = '.excellent_list .info';
        $data = $this->collect($url, $reg, $rang);
        $this->collect_redis_set($data, $site_url, 3);

        return true;
    }

    /**
     * @param $data
     * @param $site_url
     * @param int $type
     * @return bool
     * @Depiction 采集数据推入
     * @Author hwy <429208948@qq.com>
     * @Date 2022/6/17  18:53
     */
    public function collect_redis_set($data, $site_url, $type = 1)
    {
        $time = Carbon::now()->toDateTimeString();

        $cache_name = 'collect_article';

        $collect_data_title = $this->collectArticleRepository->getData();

        foreach ($data as $value) {
            $title = explode('  ', $value['title'])[0] ?? $value['title'];
            if (!in_array($title, $collect_data_title)) {
                $insert = [
                    'title'      => $title,
                    'href'       => $site_url . $value['href'],
                    'source'     => $type,
                    'created_at' => $time,
                    'updated_at' => $time,
                ];
                Redis::rpush($cache_name, json_encode($insert));
            }
        }

        return true;
    }

    public function addDataDb()
    {
        $cache_name  = 'collect_article';
        $data_len    = Redis::llen($cache_name);
        $insert_data = [];
        $i           = 1;
        while ($i <= $data_len) {
            $insert_datas          = json_decode(Redis::lPop($cache_name), true);
            $insert_datas['title'] = explode('  ', $insert_datas['title'])[0] ?? $insert_datas['title'];
            $insert_data[]         = $insert_datas;
            $i++;
        }

        if (!empty($insert_data)) {
            //缓存数据去重
            $insert_data = uniqu_arr($insert_data);
            try {
                dispatch((new CollectArticleJob($insert_data))->onQueue('default'));
                //采集数量发送钉钉
                $send_info = '(新)当前时间段采集的文章数量： ' . count($insert_data) . '篇';
                foreach ($insert_data as $value) {
                    if (!empty(trim($value['title']))) {
                        $send_info .= PHP_EOL . trim($value['title']);
                    }
                }
                $dingDing = new DingDing();
                $dingDing->textMsgSend($send_info);
            } catch (\Exception $e) {
                Log::info('collect_article send dingding error');
            }

        }
        //todo 记录写入日志
        return $data_len;
    }
}
